Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the impossible downcast of toArray(). #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Fix the impossible downcast of toArray(). #125

wants to merge 1 commit into from

Conversation

Kui-Liu
Copy link

@Kui-Liu Kui-Liu commented Oct 10, 2017

This code is casting the result of this.items.toArray() on a list to the String type.
This will usually fail by throwing a ClassCastException. The toArray() of almost all collections return an Object[]. They can't really do anything else, since the Collection object has no reference to the declared generic type of the collection.

The correct and efficient way to get a string array from a list is to use list.toArray(new String[list.size()]);.
http://findbugs.sourceforge.net/bugDescriptions.html#BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY

This code is casting the result of this.items.toArray() on a list to the String type.
This will usually fail by throwing a ClassCastException. The toArray() of almost all collections return an Object[]. They can't really do anything else, since the Collection object has no reference to the declared generic type of the collection.

The correct and efficient way to get a string  array from a list is to use list.toArray(new String[list.size()]);.
http://findbugs.sourceforge.net/bugDescriptions.html#BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant